package cn.xsly.org.service.impl;

import cn.xsly.basic.exception.BusinessException;
import cn.xsly.basic.service.impl.BaseServiceImpl;
import cn.xsly.basic.util.BaiduAiUtils;
import cn.xsly.org.domain.Employee;
import cn.xsly.org.domain.Shop;
import cn.xsly.org.domain.ShopAuditLog;
import cn.xsly.org.domain.ShopDto;
import cn.xsly.org.mapper.EmployeeMapper;
import cn.xsly.org.mapper.ShopAuditLogMapper;
import cn.xsly.org.mapper.ShopMapper;
import cn.xsly.org.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements ShopService {
@Autowired
private ShopMapper shopMapper;
@Autowired
private EmployeeMapper employeeMapper;
@Autowired
private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    @Override
    public void settlement(Shop shop) {
        String address = shop.getAddress();
        String name = shop.getName();
        String tel = shop.getTel();

        if(StringUtils.isEmpty(address)||StringUtils.isEmpty(name)|| StringUtils.isEmpty(tel) ){
            throw new BusinessException("数据不能为空");
        }
        //查询是否入驻（是否有地址和店铺名）
        Shop dbShop=shopMapper.loadByNameAndAddress(shop);
        if(dbShop!=null){
            throw new BusinessException("已入住过，忘记密码请找回密码");
        }

        if(!BaiduAiUtils.textCensor(shop.getName())){
            throw new BusinessException("名称非法了");
        }
       String imgUrl="http://123.207.27.208"+shop.getLogo();
        if(!BaiduAiUtils.imgCensor(imgUrl)){
            throw new BusinessException("图片非法了");
        }
        //保存数据
        Employee admin=shop.getAdmin();
        employeeMapper.save(admin);
        shop.setAdmin_id(admin.getId());
        shopMapper.save(shop);
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);

    }

    @Override
    public void auditPass(ShopAuditLog shopAuditLog) throws MessagingException {
        //获取店铺id在根据ID查所有数据
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        shop.setState(2);
        shopMapper.update(shop);

        //2.添加审核日志
        shopAuditLog.setState(2);
        shopAuditLog.setAuditId(325L); //写死的，因为现在没有登录，不知道是哪一个管理员审核的
        shopAuditLogMapper.save(shopAuditLog);

        //3.发送激活邮件 @TODO
        //创建复杂的邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        //设置发送方
        helper.setFrom("599271526@qq.com");
        Employee employee = employeeMapper.loadById(shop.getAdmin_id());
        //设置收件人
        helper.setTo(employee.getEmail());
        //设置主题
        helper.setSubject("店铺激活邮件");
        //设置内容
        helper.setText("<h2>店铺激活邮件<br/><a href='http://localhost:8080/shop/active/"
                +shop.getId()+"'>点击链接激活</a></h2>",true);
        //发送邮件
        javaMailSender.send(mimeMessage);

    }

    @Override
    public void auditReject(ShopAuditLog shopAuditLog) throws MessagingException {
        //1.改状态：1 - 4
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        shop.setState(4);
        shopMapper.update(shop);

        //2.添加审核日志
        shopAuditLog.setState(4);
        shopAuditLog.setAuditId(325L);
        shopAuditLogMapper.save(shopAuditLog);

        //3.发送邮件重新入驻 @TODO
        //创建复杂的邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        //设置发送方
        helper.setFrom("599271526@qq.com");
        Employee employee = employeeMapper.loadById(shop.getAdmin_id());
        //设置收件人
        helper.setTo(employee.getEmail());
        //设置主题
        helper.setSubject("店铺审核失败");
        //设置内容
        helper.setText("<h2>你的店铺审核失败，请重新入驻<br/>" +
                "<a href='http://localhost:8081/#/register'>点击链接重新入驻</a></h2>",true);
        //发送邮件
        javaMailSender.send(mimeMessage);

        //4.删除入驻信息
        employeeMapper.remove(employee.getId());
        shopMapper.remove(shop.getId());
    }

    @Override
    public List<ShopDto> echarts() {
        return shopMapper.echarts();
    }

    @Override
    public void active(Long id) {
        Shop shopId = shopMapper.loadById(id);
        shopId.setState(3);
        shopMapper.update(shopId);
    }
}
